package tt.dz.appserver.mvc.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.business.CarService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.CarControlCommand;
import tt.dz.appserver.util.enums.Response;



/**
 * 汽车控制器
 * @author xielf
 *
 */
@RestController
@RequestMapping("/car")
public class CarController {
	
	/**汽车业务接口*/
	@Resource
	private CarService carService;

	
	/**
	 * 查询车辆列表
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/carList",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String carList(){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.carList();
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 查询车辆支持的控制指令
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/controlCommand",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String controlCommand(String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCommand(vin);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 查询车辆位置
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/getCarLocation",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getCarLocation(String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.getCarLocation(vin);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 防盗报警
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/burglarAlarm",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String burglarAlarm(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.BURGLAR_ALARM.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 锁车控制
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/lockTheCar",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String lockTheCar(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.LOCK_CAR.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 后备箱解锁
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/trunkToUnlock",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String trunkToUnlock(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.TRUNK_UNLOCK.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 空调冷风
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/airConditioningColdWind",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String airConditioningColdWind(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.AIR_CONDITIONING_COLD_WIND.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 空调暖风
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/airConditioningWarmWind",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String airConditioningWarmWind(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.AIR_CONDITIONING_WARM_WIND.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 双闪控制
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/doubleFlashControl",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String doubleFlashControl(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.DOUBLE_FLASH_CONTROL.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 鸣笛控制
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/honkingControl",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String honkingControl(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.HONKING_CONTROL.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 寻车
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/findCar",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String findCar(@RequestParam(required=false) String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			//默认传1，表示开启寻车
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.FIND_CAR.getCode(), "1");
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 油路控制
	 * @param action
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/oilControl",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String oilControl(@RequestParam String action, String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.controlCar(vin, CarControlCommand.OIL_CONTROL.getCode(), action);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 路程信息
	 * @param vin
	 * @return
	 */
	@RequestMapping(value="/journeyInfo",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getJourney(@RequestParam String vin){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			resultMappingEntity = carService.journey(vin);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
		
	}
	
}
